package com.example.lancooznbk.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.model.result.BaseReviewItemEntity;
import com.example.lancooznbk.presenter.BaseReviewPresenter;
import com.example.lancooznbk.view.activity.ReviewProcessActivity;
import com.example.lancooznbk.view.activity.WrongReviewActivity;
import com.example.lancooznbk.view.adapter.BaseReviewAdapter;
import com.example.lancooznbk.view.adapter.PopupMenuType1Adapter;
import com.example.lancooznbk.view.base.BaseMainFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.widget.CustomFooterView;
import com.example.lancooznbk.view.widget.PopupMenu;
import com.jakewharton.rxbinding.view.RxView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import rx.functions.Action1;


/**
 * Created by Administrator on 2017/2/6.【基本复习】
 * modify by tangminglong
 */
public class BaseReviewFragment extends BaseMainFragment<BaseReviewPresenter> implements View.OnClickListener, ILoadMoreDataView<BaseReviewItemEntity> {



    private BaseReviewAdapter mBaseReviewAdapter;

    private TextView mTvSpStatus;
    private TextView mTvTotal;

    private PopupMenuType1Adapter popupMenuSubjectAdapter;
    private PopupMenu subjectPopupMenu;
    private View masker;
    private RelativeLayout mSpLayout;

    //private Toolbar mToolbar;



    private Map<String,Integer> mQuesStatusMap = new HashMap<>();





    @Override
    protected void updateViews(boolean refresh) {
        //setHasOptionsMenu(true);
        mPresenter.getData(refresh);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_base_review;
    }

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {
        mPresenter = new BaseReviewPresenter(this);

        //mFAB.setOnClickListener(this);
        RxView.clicks(mFAB)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        int[] startingLocation = new int[2];
                        mFAB.getLocationOnScreen(startingLocation);
                        startingLocation[0] += mFAB.getWidth() / 2;
                        WrongReviewActivity.launch(getHolderActivity(),0,startingLocation);
                    }
                });

        mSpLayout = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        mTvSpStatus = (TextView) view.findViewById(R.id.sp_status);
        mTvTotal = (TextView) view.findViewById(R.id.tv_paperNumber);
        masker = view.findViewById(R.id.bg_masker);
        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.baseReview);
        toolBarLayout.findViewById(R.id.img_main_menu).setOnClickListener(this);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);
        // mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        //initToolBar(mToolbar,false,R.string.baseReview);
        mTvSpStatus.setOnClickListener(this);
        String[] quesStatusArrays = getResources().getStringArray(R.array.QuesStatusArrays);
        String[] quesStatusNameArrays = new String[quesStatusArrays.length];
        for (int i = 0 ; i < quesStatusArrays.length ; i++){
            String[] split = quesStatusArrays[i].split("\\|");
            quesStatusNameArrays[i]  = split[0];
            mQuesStatusMap.put(split[0], Integer.valueOf(split[1]));
        }
        popupMenuSubjectAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesStatusNameArrays);
        subjectPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        subjectPopupMenu.setListAdapter(popupMenuSubjectAdapter);
        subjectPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSpStatus.setText(subjectPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByStatus(mQuesStatusMap.get(subjectPopupMenu.getCheckedStr()));
            }
        });
        mTvSpStatus.setText(subjectPopupMenu.getCheckedStr());
        subjectPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpStatus.setSelected(false);
            }
        });


        mBaseReviewAdapter = new BaseReviewAdapter(getContext());
        mBaseReviewAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mBaseReviewAdapter);
        mBaseReviewAdapter.setOnItemClickListener(new BaseReviewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<BaseReviewItemEntity.BaseReviewItemBean> list, int position) {
                //ToastUtils.showShort("第"+(position+1)+"个");
               mPresenter.openItemDetail(position,list.get(position));
            }
        });

    }


    @Override
    public void showNetError() {
        super.showNetError();
        setPaperTotal(0);

    }


    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        setPaperTotal(0);

    }

    @Override
    public void hideLoading() {
        super.hideLoading();

    }

    @Override
    public void loadData(BaseReviewItemEntity data) {
        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }

        setPaperTotal(data.getTotalCount());
        mBaseReviewAdapter.clearAndAdd(data.getModelList());
        loadComplete(mBaseReviewAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void loadMoreData(BaseReviewItemEntity data) {
        setPaperTotal(data.getTotalCount());
        mBaseReviewAdapter.add(data.getModelList());
        loadComplete(mBaseReviewAdapter.getAdapterItemCount() >= data.getTotalCount());
    }





    /**
     * 没有试卷或者加载失败的逻辑处理
     */
    private void setPaperTotal(int number) {
        String paperTotal = String.format(getString(R.string.paperTotal),number);
        mTvTotal.setText(Html.fromHtml(paperTotal));
    }



    /*@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.menu_main,menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.item_other_system){
            //ToastUtils.showShort("基本复习进度提示");

            return true;
        }
        return false;
    }*/

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.sp_status:
               masker.setVisibility(View.VISIBLE);//阴影布局可见
               mTvSpStatus.setSelected(true);
               subjectPopupMenu.showLocation(mSpLayout, 0, 0);
               break;
           case R.id.img_main_menu:
               //startActivity(new Intent(getHolderActivity(), TempActivity.class));
               startActivity(new Intent(getHolderActivity(), ReviewProcessActivity.class));
               break;
       }


    }

}